package com.whli.jee.common.web.service.impl;

import com.whli.jee.common.page.Page;
import com.whli.jee.common.util.BeanUtils;
import com.whli.jee.common.util.WebUtils;
import com.whli.jee.common.web.dao.IBaseDao;
import com.whli.jee.common.web.entity.BaseEntity;
import com.whli.jee.common.web.service.IBaseService;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>基础服务默认实现类</p>
 * @author whli
 * @version 1.0.0
 * @date 2019/6/23 11:51
 */
public abstract class BaseServiceImpl<T extends BaseEntity> implements IBaseService<T> {

    public abstract IBaseDao<T> getDao();

    @Override
    public int save(T entity) {
        entity.setId(BeanUtils.getUUID());
        String createBy = StringUtils.isBlank(WebUtils.getLoginName()) ? "anonymous": WebUtils.getLoginName();
        entity.setCreateBy(createBy);
        entity.setCreateDate(new Date());
        entity.setUpdateBy(createBy);
        entity.setUpdateDate(new Date());
        return getDao().save(entity);
    }

    @Override
    public int saveBatch(List<T> entities) {
        for (T entity : entities){
            this.save(entity);
        }
        return 1;
    }

    @Override
    public int update(T entity) {
        String updateBy = StringUtils.isBlank(WebUtils.getLoginName()) ? "anonymous": WebUtils.getLoginName();
        entity.setUpdateBy(updateBy);
        entity.setUpdateDate(new Date());
        return getDao().update(entity);
    }

    @Override
    public int updateBatch(List<T> entities) {
        for (T entity : entities){
            this.update(entity);
        }
        return 1;
    }

    @Override
    public int delete(String id) {
        return getDao().delete(id);
    }

    @Override
    public int deleteBatch(List<String> ids) {
        return getDao().deleteBatch(ids);
    }

    @Override
    public T getByPK(String id) {
        return getDao().getByPK(id);
    }

    @Override
    public List<T> listByPKs(List<String> ids) {
        return getDao().listByPKs(ids);
    }

    @Override
    public T getByNo(String no) {
        return getDao().getByNo(no);
    }

    @Override
    public T getByName(String name) {
        return getDao().getByName(name);
    }

    @Override
    public Page<T> listByPage(T entity, Page<T> page) {
        page.setPageRecords(getDao().listByPage(entity,page));
        return page;
    }

    @Override
    public Page<Map<String, Object>> listMapByPage(T entity, Page<Map<String, Object>> page) {
        page.setPageRecords(getDao().listMapByPage(entity,page));
        return page;
    }

    @Override
    public List<T> listAll(T entity) {
        return getDao().listAll(entity);
    }

    @Override
    public void download(T entity, HttpServletResponse response) {

    }

    @Override
    public void downloadTemplate(T entity, HttpServletResponse response) {

    }

    @Override
    public int upload(File file) {
        return 0;
    }

    @Override
    public int upload(InputStream stream) {
        return 0;
    }

    @Override
    public int upload(String contentType, File file) {
        return 0;
    }

    @Override
    public int upload(String contentType, InputStream stream) {
        return 0;
    }
}
